package com.example.xd.defaultcall.key_board;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.xd.defaultcall.R;
import com.example.xd.defaultcall.TelecomManagerUitls;

import java.util.ArrayList;

/**
 * @auther XuDan on 2020/6/29.
 */
public class DefaultKeyBoard extends FrameLayout implements BaseQuickAdapter.OnItemClickListener, View.OnClickListener {

    private EditText mKb_et;
    private ImageView mKb_cancle_img;
    private RecyclerView mKb_rv;
    private ImageView mKb_left_img;
    private ImageView mKe_center_img;
    private ImageView mKb_right_img;
    private ArrayList<NumItem> choiceItems = new ArrayList<>();
    Context context;
    StringBuilder sb = new StringBuilder();
    private FrameLayout mFl_show;
    private RelativeLayout mRl_bottom;
    private boolean mIs_call_ui;


    public DefaultKeyBoard(@NonNull Context context) {
        this(context, null);
    }

    public DefaultKeyBoard(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DefaultKeyBoard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        initData();
        initView(context, attrs);
    }

    private void initData() {
        choiceItems.add(new NumItem("1", "@"));
        choiceItems.add(new NumItem("2", "ABC"));
        choiceItems.add(new NumItem("3", "DEF"));
        choiceItems.add(new NumItem("4", "GHI"));
        choiceItems.add(new NumItem("5", "JKL"));
        choiceItems.add(new NumItem("6", "MNO"));
        choiceItems.add(new NumItem("7", "PQRS"));
        choiceItems.add(new NumItem("8", "TUV"));
        choiceItems.add(new NumItem("9", "WXYZ"));
        choiceItems.add(new NumItem("*", ","));
        choiceItems.add(new NumItem("0", "+"));
        choiceItems.add(new NumItem("#", ""));

    }

    private void initView(Context context, AttributeSet attrs) {
        LayoutInflater.from(context).inflate(R.layout.my_kayboard_layout, this, true);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.DefaultKeyBoard);
        boolean isShow = typedArray.getBoolean(R.styleable.DefaultKeyBoard_is_show_rl_bottom, true);
        mIs_call_ui = typedArray.getBoolean(R.styleable.DefaultKeyBoard_is_call_ui, false);


        typedArray.recycle();
        mKb_et = findViewById(R.id.kb_tv);
        mKb_et.setShowSoftInputOnFocus(false);
        mKb_cancle_img = findViewById(R.id.kb_cancle_img);
        mFl_show = findViewById(R.id.fl_show);
        mRl_bottom = findViewById(R.id.rl_bottom);
        if (isShow) {
            mRl_bottom.setVisibility(VISIBLE);
        } else {
            mRl_bottom.setVisibility(GONE);
        }
        if (mIs_call_ui) {
            mKb_cancle_img.setVisibility(GONE);
        } else {
            mKb_cancle_img.setVisibility(VISIBLE);

        }
        mKb_rv = findViewById(R.id.kb_rv);
        mKb_left_img = findViewById(R.id.kb_left_img);
        mKe_center_img = findViewById(R.id.ke_center_img);
        mKb_right_img = findViewById(R.id.kb_right_img);
        mKb_cancle_img.setOnClickListener(this);
        mKe_center_img.setOnClickListener(this);
        mKb_left_img.setOnClickListener(this);
        mKb_right_img.setOnClickListener(this);
        mKb_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (mKb_et.getText().toString().length() > 0) {
                    mFl_show.setVisibility(VISIBLE);
                } else {
                    mFl_show.setVisibility(INVISIBLE);

                }
            }
        });


        initRv(context);
    }

    private void initRv(Context context) {
        mKb_rv.setLayoutManager(new GridLayoutManager(context, 3));
        mKb_rv.addItemDecoration(new MyDecoration(context, LinearLayoutManager.VERTICAL));
        mKb_rv.addItemDecoration(new MyDecoration(context, LinearLayoutManager.HORIZONTAL));
        MyAdapter myAdapter = new MyAdapter(R.layout.num_item, choiceItems);
        mKb_rv.setAdapter(myAdapter);
        myAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        sb.append(choiceItems.get(position).num);
        setText(sb.toString());
    }

    private void setText(String s) {
        mKb_et.setText(s);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.kb_cancle_img:
                cancleNum();
                break;
            case R.id.ke_center_img:
                callPhone();
                break;
            case R.id.kb_left_img:
                left();
                break;
            case R.id.kb_right_img:
                right();
                break;
        }
    }

    private void right() {
    }

    private void left() {
    }

    private void callPhone() {
        String phoneNum = mKb_et.getText().toString();
        TelecomManagerUitls instance = TelecomManagerUitls.getInstance(context);
        instance.call(phoneNum);

    }

    private void cancleNum() {
        int selectionStart = mKb_et.getSelectionStart();
        int selectionEnd = mKb_et.getSelectionEnd();

        Log.i("光标位置---", selectionStart + "--" + selectionEnd);
        if (selectionEnd == 0) {
            if (sb.length() > 0) {
                sb.deleteCharAt(sb.length() - 1);
                setText(sb.toString());
            }
        } else {
            if (sb.length() > 0) {
                sb.deleteCharAt(selectionEnd - 1);
                setText(sb.toString());
                mKb_et.setSelection(selectionStart - 1);
            }
        }
    }
}
